package com.weiyun.controller;

import com.weiyun.common.R;
import com.weiyun.pojo.Cart;
import com.weiyun.pojo.DeliveryAddress;
import com.weiyun.pojo.User;
import com.weiyun.service.CartService;
import com.weiyun.service.DeliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/deliveryAddress")
@CrossOrigin
public class DeliveryAddressController {
    @Autowired
    private DeliveryAddressService deliveryAddressService;
    @PostMapping("/listDeliveryAddressByUserId")
    public R listDeliveryAddressByUserId(String userId) {
        try {
            List<DeliveryAddress> deliveryAddresses = deliveryAddressService.listDeliveryAddressByUserId(userId);
            return R.ok().data(deliveryAddresses);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/saveDeliveryAddress")
    public R addUserAddress(DeliveryAddress deliveryAddress) {
        try {
            int i = deliveryAddressService.saveDeliveryAddress(deliveryAddress);
            if (i > 0) {
                return R.ok().data(1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.error().data(0);
    }
    @PostMapping("/removeDeliveryAddress")
    public R removeDeliveryAddress(Integer daId) {
        try {
            int i = deliveryAddressService.removeDeliveryAddress(daId);
            if (i > 0) {
                return R.ok().data(1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.error().data(0);
    }
    @PostMapping("/getDeliveryAddressById")
    public R getDeliveryAddressById(User userId) {
        try {
            DeliveryAddress deliveryAddressById = deliveryAddressService.getDeliveryAddressById(userId);
            return R.ok().data(deliveryAddressById);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/updateDeliveryAddress")
    public R updateDeliveryAddress(DeliveryAddress deliveryAddress) {
        try {
            System.out.println(deliveryAddress);
            int i = deliveryAddressService.updateDeliveryAddress(deliveryAddress);
            return R.ok().data(i);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}